package com.cybertron.ironhide.manage.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.cybertron.ironhide.manage.common.base.BaseAction;
import com.cybertron.ironhide.manage.domain.Category;
import com.cybertron.ironhide.manage.domain.SeedUrl;
import com.cybertron.ironhide.manage.service.CategoryService;
import com.cybertron.ironhide.manage.service.SeedUrlService;

public class CategoryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2237670374873915031L;

	@Resource
	private CategoryService categoryService;

	@Resource
	private SeedUrlService seedUrlService;

	private List<Category> categories;

	private Category category;

	private List<SeedUrl> seedUrls;

	private SeedUrl seedUrl;

	private File seedFile;

	private String seedFileFileName;

	public String view() {
		Category root = new Category();
		root.setTitle("分类列表");
		root.setId("root");
		root.setChilds(categoryService.selectRootCategories());
		categories = new ArrayList<Category>();
		categories.add(root);
		return VIEW;
	}

	public String list() {
		categories = categoryService.selectByParentId(category.getId(), pager);
		if ("root".equals(category.getId())) {
			return LIST;
		}
		category = categoryService.selectByPrimaryKey(category.getId());
		if (category != null && category.getIsFinal()) {
			seedUrls = seedUrlService.selectByCategoryId(category.getId(),
					pager);
			return "seed_list";
		}
		return LIST;
	}

	public String add() {
		return ADD;
	}

	public String seedAdd() {
		return "seed_add";
	}

	public String seedFile() {
		return "seed_file";
	}

	public String doSeedAdd() {
		seedUrlService.insertSelective(seedUrl);
		return ajaxSuccess("添加成功", "category-view", "cagegory-list-box",
				CLOSE_CURRENT);
	}

	public String doSeedAddByFile() {
		String extension = StringUtils
				.substringAfterLast(seedFileFileName, ".");
		if (!"xlsx".equals(extension.toLowerCase())
				&& !"xls".equals(extension.toLowerCase())) {
			return ajaxError("上传文件格式错误", null, null, CLOSE_CURRENT);
		}
		seedUrlService.insertSelective(seedFile);
		return ajaxSuccess("添加成功", "category-view", "cagegory-list-box",
				CLOSE_CURRENT);
	}

	public String seedDelete() {
		seedUrlService.deleteByPrimaryKey(seedUrl.getId());
		return ajaxSuccess("添加成功", "category-view", "cagegory-list-box");
	}

	public String doAdd() {
		categoryService.insertSelective(category);
		return ajaxSuccess("添加成功", "category-view", null, CLOSE_CURRENT);
	}

	public String edit() {
		category = categoryService.selectByPrimaryKey(category.getId());
		return EDIT;
	}

	public String doEdit() {
		categoryService.updateByPrimaryKeySelective(category);
		return ajaxSuccess("修改成功", "category-view", null, CLOSE_CURRENT);
	}

	public String delete() {
		categoryService.deleteByPrimaryKey(category.getId());
		return ajaxSuccess("删除成功", "category-view");
	}

	public String taskAdd() {
		categoryService.insertTaskSelective(category.getId());
		return ajaxSuccess("添加任务成功", "category-view", "cagegory-list-box");
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<SeedUrl> getSeedUrls() {
		return seedUrls;
	}

	public void setSeedUrls(List<SeedUrl> seedUrls) {
		this.seedUrls = seedUrls;
	}

	public SeedUrl getSeedUrl() {
		return seedUrl;
	}

	public void setSeedUrl(SeedUrl seedUrl) {
		this.seedUrl = seedUrl;
	}

	public File getSeedFile() {
		return seedFile;
	}

	public void setSeedFile(File seedFile) {
		this.seedFile = seedFile;
	}

	public String getSeedFileFileName() {
		return seedFileFileName;
	}

	public void setSeedFileFileName(String seedFileFileName) {
		this.seedFileFileName = seedFileFileName;
	}

}
